<?php
/**
 * Created by PhpStorm.
 * User: 罗俊阳
 * Date: 2018/5/5 0005
 * Time: 09:23
 */

namespace app\admin\controller;


use app\admin\model\Tenantdistributionlinkstore;
use app\api\service\Token;
use app\common\model\Tenant;
use controller\BasicAdmin;
use distribution\DadaAddOrder;
use function Psy\sh;
use think\Model;
use think\Request;
use think\response\Redirect;
use traits\controller\jump;
use app\admin\model\Tenantdistributiontype as TenantdistributiontypeModel;
//配送分类
class TenantDistributionType extends Base
{
    /**
     * @return mixed|void
     * 获取商户配送信息
     */
    public function index(){
       $Tenant= Tenant::where('id',session('TENANT_ID'))->field('id,is_tdt')->find();
       $tdtDate = new TenantdistributiontypeModel();
       $cuTdTypes = $tdtDate->findCuTenantTdTypes();
       $self_Type_id = null;
        $logistics_type_id = null;
        $self_type_status = 0;
        $logistics_type_status = 0;
        if($cuTdTypes && !empty($cuTdTypes)){
            foreach($cuTdTypes as $item){
                if($item->distributionType == TenantdistributiontypeModel::Type_1){
                    $self_Type_id = $item['id'];
                    $self_type_status = $item['status'];

                }else{
                    $logistics_type_id = $item['id'];
                    $logistics_type_status = $item['status'];
                }
            }
        }else{
            return $this->redirect('/index/index');
        }
        $this->assign('self_type_status',$self_type_status);
        $this->assign('logistics_type_status',$logistics_type_status);
        $this->assign('self_type_id',$self_Type_id);
        $this->assign('logistics_type_id',$logistics_type_id);
        $this->assign('is_tdt',$Tenant['is_tdt']);

        return $this->fetch();
    }

    /**
     * @return mixed
     * 自取页面
     */
    public function shop_extraction(){
        $typeId = Request::instance()->param('type_id');
        $tenantId = session("TENANT_ID");
        $stores = \app\admin\model\Store::where(['tenantId'=>$tenantId,'isActive'=>1])->select();
        //获取商户所关联门店
        $tdtModel = new \app\admin\model\Tenantdistributiontype();
        $tdt = $tdtModel->findTdtType($typeId);
        $status = $tdt['status'];
        $packingCharge = $tdt['packingCharge'];
        $applicableType = $tdt['applicableType'];
        $currentStores = $tdtModel->getCurrentStores($typeId);
        $storeIds = array_column($currentStores, 'id');

        $this->assign("storeIds", $storeIds);
        $this->assign("currentStores",json_encode($currentStores));
        $this->assign("status",$status);
        $this->assign("packingCharge",$packingCharge/100);
        $this->assign("applicableType",$applicableType);
        $this->assign("stores",$stores);
        $this->assign("typeId",$typeId);

        return $this->fetch();
    }

    /**
     *
     */
    public function ajax_get_stores(){
        $tenantId = session("TENANT_ID");
        $tenantModel = new Tenant() ;
    }

    /**
     * @return \think\response\Json
     * 自取类型设置
     */
   public function ajax_add_or_update_self(){
       $status = input("post.is_enable");
       $applicableType = input("post.shop_type");
       $packingCharge = input("post.packingCharge")*100;
       $type_id = input('post.type_id');
       if(input('post.stores/a')){

           $storeIdsArr = $this->StringTOarr(input('post.stores/a'));
       }
       $tdtModel = new \app\admin\model\Tenantdistributiontype();
       $tdtRes = $tdtModel->updateTdtType($status,$applicableType,$type_id,$packingCharge);

       if($applicableType==$tdtModel::APP_TYPE_2){

           $TdtStoreModel = new Tenantdistributionlinkstore();

           $tdtRes = $TdtStoreModel->add_or_update_store($storeIdsArr,$type_id,$packingCharge);

       }
       return show($tdtModel::STATUS_VALID,"新增修改成功",$tdtRes);
   }

   private function StringTOarr($data){
       if(!is_array($data)){
           $data = [
               0 => $data,
           ];
       }
       return $data;
   }

    /**
     * @return \think\response\Json
     * 设置商户配送
     */
    public function ajax_setting(){
       $tenantId = session("TENANT_ID");
       $tenantModel = new \app\admin\model\Tenantdistributiontype();
       $res = $tenantModel->add_setting($tenantId);
        if($res){
            $msg = "恭喜您设置成功";
        }else{
            $msg = "抱歉，您已经设置过配送！";
        }
        return show($tenantModel::STATUS_VALID,$msg,$res);
    }


}